package com.example.audiostreaming.implement;

import com.example.audiostreaming.data.XimalayApi;
import com.example.audiostreaming.interfaces.IDetailLogic;
import com.example.audiostreaming.interfaces.IDetailViewCallback;
import com.example.audiostreaming.utils.UtilConstants;
import com.example.audiostreaming.utils.UtilLog;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.audiostreaming.data.XimalayApi.getXimalayApi;

/**
 * 详情界面逻辑实现层
 * 详情界面相关逻辑接口类的实现类
 */
public class ImplDetailLogic implements IDetailLogic {
    private static final String TAG = "ImplDetailLogic";
    private List<IDetailViewCallback> mIDetailViewCallbacks = new ArrayList<>();//回调过来的视图
    private List<Track> mTracks = new ArrayList<>();

    private Album mDetailAlbum =null;//目标专辑
    private int mCurrentDetailAlbumId =-1;
    private int mCurrentPage=0;

    private ImplDetailLogic(){}
    private static ImplDetailLogic sInstanceImplDetailLogic = null;
    /**
     * 懒汉式单例模式
     * @return 单例对象
     */
    public static ImplDetailLogic getInstanceImplDetailLogic(){
        if (sInstanceImplDetailLogic ==null) {
            synchronized (ImplDetailLogic.class){//防止多线程
                if (sInstanceImplDetailLogic ==null) {
                    sInstanceImplDetailLogic = new ImplDetailLogic();
                }
            }
        }
        return sInstanceImplDetailLogic;
    }



    //设置详情专辑
    public void setDetailAlbum(Album detailAlbum){
        this.mDetailAlbum = detailAlbum;
    }

    /**
     * 获取详情专辑中的条目列表数据内容
     */
    @Override
    public void getDetailAlbumListData(int detailAlbumId, int page) {
        mTracks.clear();
        this.mCurrentDetailAlbumId = detailAlbumId;
        this.mCurrentPage = page;
        handleLoading();
        doLoaded(false);
    }


    @Override
    public void pullUpLoadMoreDetailAlbumListData() {
        mCurrentPage++;
        doLoaded(true);
    }

    @Override
    public void dropDownRefreshDetailAlbumListData() {

    }

    /**
     * @param isLoadMore 传入true,表示结果会追加到列表的后方
     */
    private void doLoaded(final boolean isLoadMore){
        XimalayApi ximalayApi = XimalayApi.getXimalayApi();
        ximalayApi.getDetailList(new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                if (trackList != null) {
                    List<Track> tracks = trackList.getTracks();
                    UtilLog.d(TAG,"曲目列表尺寸 ==> "+tracks.size());
                    if (isLoadMore) {
                        //上拉加载，结果放到后面去
                        mTracks.addAll(tracks);
                        int tracksSize = tracks.size();
                        handleLoadMoreResult(tracksSize);
                    }else {
                        //下拉刷新，结果放到前面去
                        mTracks.addAll(0, tracks);
                    }
                    handleResultDetailAlbumListData(mTracks);
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                if (isLoadMore) {
                    mCurrentPage--;
                }
                UtilLog.d(TAG,"错误码 ==> "+errorCode);
                UtilLog.d(TAG,"错误信息 ==> "+errorMsg);
                handleError();
            }
        },mCurrentDetailAlbumId,mCurrentPage);
    }

    /**
     * 处理上拉加载更多的结果
     * @param tracksSize
     */
    private void handleLoadMoreResult(int tracksSize) {
        if (mIDetailViewCallbacks != null) {
            for (IDetailViewCallback iDetailViewCallback : mIDetailViewCallbacks) {
                iDetailViewCallback.onLodeMoreFinish(tracksSize);
            }
        }
    }

    @Override
    public void registerViewCallback(IDetailViewCallback iDetailViewCallback) {
        if (mIDetailViewCallbacks != null&&!mIDetailViewCallbacks.contains(iDetailViewCallback)) {
            mIDetailViewCallbacks.add(iDetailViewCallback);
            if (mDetailAlbum != null) {
                iDetailViewCallback.onSetDetailAlbumUI(mDetailAlbum);
            }
        }
    }

    @Override
    public void unRegisterViewCallback(IDetailViewCallback iDetailViewCallback) {
        if (mIDetailViewCallbacks != null) {
            mIDetailViewCallbacks.remove(iDetailViewCallback);
        }
    }









    /**
     * 处理详情专辑列表数据结果
     * @param tracks
     */
    private void handleResultDetailAlbumListData(List<Track> tracks) {
        if (tracks != null) {
            if (tracks.size()==0) {
                if (mIDetailViewCallbacks != null) {
                    for (IDetailViewCallback iDetailViewCallback : mIDetailViewCallbacks) {
                        iDetailViewCallback.onEmpty();
                    }
                }
            }else {
                if (mIDetailViewCallbacks != null) {
                    for (IDetailViewCallback iDetailViewCallback : mIDetailViewCallbacks) {
                        iDetailViewCallback.onSetDetailAlbumListData(tracks);
                    }
                }
            }
        }
    }

    private void handleLoading() {
        if (mIDetailViewCallbacks != null) {
            for (IDetailViewCallback iDetailViewCallback : mIDetailViewCallbacks) {
                iDetailViewCallback.onLoading();
            }
        }
    }

    private void handleError() {
        if (mIDetailViewCallbacks != null) {
            for (IDetailViewCallback iDetailViewCallback : mIDetailViewCallbacks) {
                iDetailViewCallback.onError();
            }
        }
    }


}
